package com.sholmes.android.scribbler.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

@SuppressWarnings("unused")
public class ManualDriveView extends View{	
	private static final String TAG = "ManualDriveView";
	
	private Integer _speed;
	private Integer _rotation;
	private String _message;
	private byte[] _sensors;
	
	//Used to indicate how large the transparent
	//rectangles need to be based off of IR
	//sensor readings.  The close a object is
	//the larger the rectangle and the stronger
	//the color the transparent layer becomes.
	private byte leftIRSensorReading;
	private byte centerIRSensorReading;
	private byte rightIRSensorReading;
	
	
	
	public ManualDriveView(Context context) {
		super(context);
		init();
	}
	
	public ManualDriveView(Context context,AttributeSet attrs){
		super(context,attrs);
		init();
	}
	
	public ManualDriveView(Context context,AttributeSet attrs,int defaultStyle){
		super(context,attrs,defaultStyle);
		init();
	}
	
	
	private void init(){
		this._message = "";
		this._speed = 0;
		this._rotation = 0;
		this.rightIRSensorReading=0;
		this.leftIRSensorReading=0;
		this.centerIRSensorReading=0;
		
	}



	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int height = getMeasuredHeight();
		int width = getMeasuredWidth();
		
		int opacity = 100; 		// 50% transparent 
		
		

		Paint message = new Paint(Paint.ANTI_ALIAS_FLAG);		
		message.setTextSize(80);
		message.setColor(Color.RED);		
		String messageText = this._message;

		Paint p = new Paint();
		p.setTextSize(40);
   		String speedText = _speed.toString();
   		String rotationText = _rotation.toString();
   		
   		canvas.drawText(speedText , 15, height-15, p);
   		canvas.drawText(rotationText, 480, height-15, p);
   		canvas.drawText(messageText, width/5, height/2, message);
   		
   		//Draw 3 rectangles across the entire width of the screen. 
   		Paint leftSensor = new Paint();
   		Paint centerSensor = new Paint();
   		Paint rightSensor = new Paint();
   	
   		//TODO: Set colors based off of sensor readings
   		
   		leftSensor.setColor(Color.GREEN);
   		rightSensor.setColor(Color.RED);
   		centerSensor.setColor(Color.YELLOW);
//   		Rect leftLayer = new Rect(0,0,width/3,5*this._speed);
//   		Rect centerLayer = new Rect(width/3,0,3*width/3,5*this._speed);
//   		Rect rightLayer = new Rect(2*width/3,0,3*width/3,5*this._speed);
   		Rect leftLayer = new Rect(0,0,width/3,height/3);
		Rect centerLayer = new Rect(width/3,0,3*width/3,height/3);
		Rect rightLayer = new Rect(2*width/3,0,3*width/3,height/3);
		
   		
   		leftSensor.setStyle(Style.FILL);
   		leftSensor.setAlpha(opacity);
   		
   		centerSensor.setStyle(Style.FILL);
   		centerSensor.setAlpha(opacity);
   		
   		rightSensor.setStyle(Style.FILL);
   		rightSensor.setAlpha(opacity);
   		
   		
   		
   		canvas.drawRect(leftLayer,leftSensor);
   		canvas.drawRect(centerLayer,centerSensor);
   		canvas.drawRect(rightLayer,rightSensor);
	}

	
	
	
	public void setMessage(String message){
		this._message = message;
	}

	public Integer getSpeed() {
		return _speed;
	}


	public void setSpeed(Integer speed) {
		this._speed = speed;
	}


	public Integer getRotation() {
		return _rotation;
	}


	public void setRotation(Integer rotation) {
		this._rotation = rotation;
	}

	

	public void setSensors(byte[] sensors){
		Log.d(TAG,"Sensors updated");
		this._sensors= sensors;
	
		sensors[0] = leftIRSensorReading;
		sensors[1] = centerIRSensorReading;
		sensors[2] = rightIRSensorReading;
	}
	
	
}
